package com.zeratul.common.mybatis.core.page.aop;

import com.zeratul.common.mybatis.core.page.annotation.Paging;
import com.zeratul.common.mybatis.core.page.entity.Page;
import com.zeratul.common.mybatis.core.page.utils.ZtPages;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 分页的AOP拦截
 */
@Aspect
public class PageAop {

    /**
     * 被@Paging注解标记的方法，会被AOP拦截
     * @return
     */
    @Before("@annotation(paging)")
    public void pageHandler(JoinPoint joinPoint, Paging paging) throws Throwable {

        //判断是否存在Page对象，如果不存在就直接创建
        Page page = ZtPages.getPage();
        if (page != null) {
            //开启分页
            page.setEnable(true);
        }
    }


    /**
     * 后置处理，关闭分页
     * @param joinPoint
     * @param paging
     * @throws Throwable
     */
    @Before("@annotation(paging)")
    public void afterHandler(JoinPoint joinPoint, Paging paging) throws Throwable {
        Page page = ZtPages.getPage();
        //关闭分页
        if (page != null) {
            page.setEnable(false);
        }
    }

}
